package com.ef.web.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import com.ef.constant.ETimeline;
import com.ef.domain.Leads;
import com.ef.service.ClassArrangeService;
import com.ef.service.LeadsClassEntityService;
import com.ef.service.LeadsService;

/**
 * 预订时的Class Entity 组合。<br/>
 * 
 * @author CYG create:2009-12-13 上午01:20:17
 */
public class BookingClassEntityUnit {

	private ClassArrangeService classArrangeService;

	private LeadsClassEntityService leadsClassEntityService;

	private LeadsService leadsService;

	public LeadsService getLeadsService() {
		return leadsService;
	}

	public void setLeadsService(LeadsService leadsService) {
		this.leadsService = leadsService;
	}

	public LeadsClassEntityService getLeadsClassEntityService() {
		return leadsClassEntityService;
	}

	public void setLeadsClassEntityService(
			LeadsClassEntityService leadsClassEntityService) {
		this.leadsClassEntityService = leadsClassEntityService;
	}

	public ClassArrangeService getClassArrangeService() {
		return classArrangeService;
	}

	public void setClassArrangeService(ClassArrangeService classArrangeService) {
		this.classArrangeService = classArrangeService;
	}

	private HashMap<String, HashMap<ETimeline, HashMap<Long, BookingClassEntity>>> allClassEntity = new HashMap<String, HashMap<ETimeline, HashMap<Long, BookingClassEntity>>>();

	private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

	/**
	 * @param year
	 * @param week
	 * @param leads
	 */
	public void init(int year, int week, Leads leads) {
		// 全部课程,按日期以及时间进行了排序.
//	 
//		List<ClassEntity> arrangedClass = classArrangeService.getClassEntity(
//				year, week);
//		if (arrangedClass==null||arrangedClass.size()==0){
//			return ;
//		}
//		// 当前学生选的课程
//		HashMap<ClassEntity, LeadsClassEntity> leadsClassMap = leadsClassEntityService
//				.getLeadsClassEntityMap2(year, week, leads);
//		Date oldDate = null;
//		Date newDate = null;
//		//时间段 ,老师, 课程
//		HashMap<ETimeline, HashMap<Long, BookingClassEntity>> tmpDateMap = null;
//		BookingClassEntity bookingClass = null;
//		ETimeline oldTime = null;
//		ETimeline newTime = null;
//		// 老师和选课的Map
//		HashMap<Long, BookingClassEntity> tmpTeacherClassMap = null;
//		//System.out.println("arrangedClass:" + arrangedClass.size());
//		for (ClassEntity classEntity : arrangedClass) {
//			//添加选择该课程的学生
//			List<LeadsClassEntity> allleadsClasses=
//				leadsClassEntityService.getLeadsClassEntityListWithClassEntity(classEntity);
//			if (leadsClassMap.containsKey(classEntity)) {// 学生选了这门课
//				LeadsClassEntity leadsClass = leadsClassMap.get(classEntity);
//				bookingClass = new BookingClassEntity(leadsClass,allleadsClasses);
//			} else {
//				bookingClass = new BookingClassEntity(classEntity,allleadsClasses,leads);
//			}
//			newDate = classEntity.getClassDate();
//			newTime = classEntity.getClassTime();
//			if (!newDate.equals(oldDate) // 不同天
//					|| !newTime.equals(oldTime)) {// 不同时间段
//				if (tmpTeacherClassMap != null) {
//					tmpDateMap.put(oldTime, tmpTeacherClassMap);
//				}
//				tmpTeacherClassMap = new HashMap<Long, BookingClassEntity>();
//				oldTime = newTime;
//			}
//			tmpTeacherClassMap.put(classEntity.getTeacher().getId(),
//					bookingClass);
//			if (!newDate.equals(oldDate)) {// new Date
//				if (tmpDateMap != null) {
//					allClassEntity.put(dateFormat.format(oldDate), tmpDateMap);
//					// System.out.println("init:" + oldDate);
//				}
//				tmpDateMap = new HashMap<ETimeline, HashMap<Long, BookingClassEntity>>();
//				oldDate = newDate;
//			}
//		}// end for
//		tmpDateMap.put(oldTime, tmpTeacherClassMap);
//		allClassEntity.put(dateFormat.format(oldDate), tmpDateMap);
		// System.out.println("init:"+oldDate);
	}

	public BookingClassEntity getClassEntity(Date date, ETimeline timeline,
			String teacherid) {
		HashMap<ETimeline, HashMap<Long, BookingClassEntity>> dateMap = allClassEntity
				.get(dateFormat.format(date));
		//System.out.println("getClassEntity:"+date);

		if (dateMap != null) {
			HashMap<Long, BookingClassEntity> timeMap = dateMap.get(timeline);
			if (timeMap != null) {
				BookingClassEntity entity = timeMap
						.get(Long.valueOf(teacherid));
				if (entity != null) {
					return entity;
				}
			}
		}
		return null;
	}

	/**
	 * 缓存一下,能省就省点.
	 */
	private BookingClassEntity oldEntity;

	private Date olddate;

	private ETimeline oldtime;

	private String oldteacher;

	public String getClassEntityInfo(Date date, ETimeline timeline,
			String teacherid) {
		if (!date.equals(olddate) || !timeline.equals(oldtime)
				|| !teacherid.equals(oldteacher)) {
			oldEntity = getClassEntity(date, timeline, teacherid);
			olddate = date;
			oldtime = timeline;
			oldteacher = teacherid;
		}
		if (oldEntity != null) {
			return oldEntity.getMessage();
		} else {
			return "";
		}
	}
	
	public String getClassEntityColor(Date date, ETimeline timeline,
			String teacherid) {
		if (!date.equals(olddate) || !timeline.equals(oldtime)
				|| !teacherid.equals(oldteacher)) {
			oldEntity = getClassEntity(date, timeline, teacherid);
			olddate = date;
			oldtime = timeline;
			oldteacher = teacherid;
		}
		if (oldEntity != null) {
			return oldEntity.getStatusColor();
		} else {
			return "";
		}
	}
	
	public String getClassEntityToolTip(Date date, ETimeline timeline,
			String teacherid) {
		if (!date.equals(olddate) || !timeline.equals(oldtime)
				|| !teacherid.equals(oldteacher)) {
			oldEntity = getClassEntity(date, timeline, teacherid);
			olddate = date;
			oldtime = timeline;
			oldteacher = teacherid;
		}
		if (oldEntity != null) {
			return oldEntity.getToolTip();
		} else {
			return "";
		}
	}
	
	public String getClassEntityId(Date date, ETimeline timeline,
			String teacherid) {
		if (!date.equals(olddate) || !timeline.equals(oldtime)
				|| !teacherid.equals(oldteacher)) {
			oldEntity = getClassEntity(date, timeline, teacherid);
			olddate = date;
			oldtime = timeline;
			oldteacher = teacherid;
		}
		if (oldEntity != null) {
			return oldEntity.getClassentity().getId().toString();
		} else {
			return "-1";
		}
	}
}
